import { _decorator, AudioClip, Component, Node, sp } from 'cc';
import { Constant } from './Constant';
import { AudioMgr } from './AudioMgr';
const { ccclass, property } = _decorator;

const AnimationList = ['idle1', 'appear', 'guangbi']
@ccclass('FrontBg')
export class FrontBg extends Component {
    @property(sp.Skeleton)
    public skAnimation: sp.Skeleton;
    @property(AudioClip)
    public openFbg: AudioClip;
    @property(AudioClip)
    public closeFbg: AudioClip;
    private _callBack: Function;
    private _thisOrg: any;

    start() {

    }

    public playAnimation(index: number, deley?: number, callBack?: Function, thisArg?: any) {
        this._callBack = callBack;
        this._thisOrg = thisArg;
        if (index < 0 || index >= AnimationList.length) {
            console.error('无效的动画索引');
            return;
        }
        if (deley) {
            this.scheduleOnce(() => {
                if (index === Constant.FrontBgAni.Appear) {
                    AudioMgr.inst.playOneShot(this.openFbg);
                } else if (index === Constant.FrontBgAni.Close) {
                    AudioMgr.inst.playOneShot(this.closeFbg);
                }
                this.skAnimation.setAnimation(0, AnimationList[index], false);
                this._callBack?.call(thisArg);
            }, deley)
        } else {
            if (index === Constant.FrontBgAni.Appear) {
                AudioMgr.inst.playOneShot(this.openFbg);
            } else if (index === Constant.FrontBgAni.Close) {
                AudioMgr.inst.playOneShot(this.closeFbg);
            }
            this.skAnimation.setAnimation(0, AnimationList[index], false);
            this._callBack.call(thisArg)
        }
    }




    update(deltaTime: number) {

    }
}


